﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;

namespace yq.ControllerBase.ClassLib
{
    public class UnityControllerFactory : DefaultControllerFactory
    {
       /// <summary>
       /// 
       /// </summary>
       /// <param name="requestContext"></param>
       /// <param name="controllerName"></param>
       /// <returns></returns>
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException("requestContext");
            }
            if (String.IsNullOrEmpty(controllerName))
            {
                throw new ArgumentException("controllerName");
            }

            //不要修改
            Type controllerType = GetControllerType(requestContext, controllerName);

            object action=null;
            requestContext.RouteData.Values.TryGetValue("action", out action);

            //当没有控制器时路由到默认控制器
            if (controllerType == null && (controllerName == null || controllerName.IndexOf(".") == -1) && (action == null || action.ToString().IndexOf(".") == -1))
            {
                controllerType = GetControllerType(requestContext, WebConfig.DefaultController);
            }

            //不要修改
            return GetControllerInstance(requestContext, controllerType);
        
        }
    }
}
